﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using mySite.Core.Model;
using System.Collections;

namespace mySite.Data.MongoDB
{
    public class CategoryRepository
    {
        private Database db = null;

        public CategoryRepository(Database db)
        {
            this.db = db;
            if (db == null)
                throw new NullReferenceException("Database should not be null");
        }

        public void Add(PostCategory postCategory)
        {
            var categories = db.GetCollection("PostCategory");

            var docCat = new Document();
            docCat["Name"] = postCategory.Name;
            docCat["Created"] = postCategory.CreatedAt;

            categories.Insert(docCat);
        }

        public PostCategory Get(string name)
        {
            var spec = new Document();
            spec["Name"] = name;
            var cat = db.GetCollection("PostCategory").FindOne(spec);

            PostCategory category = new PostCategory();
            category.Name = (string)cat["Name"];
            category.Id = cat["_id"];

            return category;
        }

        public IEnumerable<Document> All()
        {
            return db.GetCollection("PostCategory").FindAll().Documents;
        }
    }
}
